package com.cos.shop.web.controller;

import com.cos.shop.common.Const;
import com.cos.shop.entity.Product;
import com.cos.shop.services.ProductServices;
import com.cos.shop.services.impl.ProductServicesImpl;
import com.cos.shop.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 *
 * @author: COS
 * @time: 2022/2/24 9:08
 * @description:
 */
@WebServlet("/pro")
public class ProductController extends BaseServlet {

    private ProductServices ps = new ProductServicesImpl();
    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/2/24 10:13
     * @description: 使用商品id获取商品详情
     */
    public String getGoodsById(HttpServletRequest request, HttpServletResponse response){
        //1 获取用户数据
        int pid = StringUtils.toInt(request.getParameter("id"));
        //2请求 商品数据
        Product pro = ps.getProductById(pid);
        //3 放入request作用域
        request.setAttribute("goods", pro);
        //4 转发
        return Const.FORWARD+"goodsDetail.jsp";
    }

    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/2/24 9:10
     * @description: 通过商品类型 id 查询商品数据
     */
    public String getProductByTid(HttpServletRequest request, HttpServletResponse response){
        //1 接受用户提交数据,并转为int类型
        int typeid = StringUtils.toInt(request.getParameter("typeid"));
        //获取分页参数
        int pageIndex= StringUtils.toInt(request.getParameter("pageIndex"));
        //2 访问业务层获取商品数据
        //List<Product> proList = ps.getProductByTid(typeid);
        Map<String, Object> map = ps.getProductWithPageByTid(pageIndex, typeid);
        Map<String,Object> maps = ps.getProductWithPageByTid(pageIndex,typeid);
        //3 存放数据到request作用域中
//        request.setAttribute("glist",proList);
        request.setAttribute("map", map);
        //4 转发goodsList.jsp
        return Const.FORWARD+"goodsList.jsp";
    }

}
